package com.tianpengtech.web.config;


import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;
import com.mysql.jdbc.Connection;
import com.tianpengtech.web.Interceptor.BasPathInterceptors;
import com.tianpengtech.web.controller.MRoomController;
import com.tianpengtech.web.handler.XssHandler;
import com.tianpengtech.web.partybulid.model._MappingKit;
import com.tianpengtech.web.plugin.ImStartPlugin;

public class ImConfig extends JFinalConfig{
	
	private static Prop p = loadConfig();

	@Override
	public void configConstant(Constants me) {
		me.setViewType(ViewType.JFINAL_TEMPLATE);
	}

	@Override
	public void configRoute(Routes me) {
		
		//me.add("/", ImController.class);
		me.add("/room", MRoomController.class,"");
	}

	@Override
	public void configEngine(Engine me) {
		me.setDevMode(true);
	}

	@Override
	public void configPlugin(Plugins me) {
		DruidPlugin druidPlugin = getDruidPlugin1();
	    me.add(druidPlugin);
	    ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
	    
	    arp.setTransactionLevel(Connection.TRANSACTION_READ_COMMITTED);
	    _MappingKit.mapping(arp);
	    // 强制指定复合主键的次序，避免不同的开发环境生成在 _MappingKit 中的复合主键次序不相同
	    // arp.setPrimaryKey("document", "mainMenu,subMenu");
	    me.add(arp);
        if (p.getBoolean("devMode", false)) {
            arp.setShowSql(true);
        }
        
       /* DruidPlugin druidPlugin1 = getDruidPlugin1();
	    me.add(druidPlugin1);
	    ActiveRecordPlugin arp1 = new ActiveRecordPlugin("test", druidPlugin1);
	    com.tianpengtech.web.partybulid.model._MappingKit.mapping(arp1);
	    me.add(arp1);*/
	    
		me.add(new ImStartPlugin());
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new SessionInViewInterceptor());
		me.add(new BasPathInterceptors());
	}

	@Override
	public void configHandler(Handlers me) {
		
		me.add(new XssHandler());
	}
	
	public static void main(String[] args) {
		
		JFinal.start("src/main/webapp", 8080, "/",5);
	}
	
	/**
     * 抽取成独立的方法，例于 _Generator 中重用该方法，减少代码冗余
     */
	public static DruidPlugin getDruidPlugin() {
		return new DruidPlugin(p.get("jdbcUrl"), p.get("user"), p.get("password").trim());
	}
	public static DruidPlugin getDruidPlugin1() {
		return new DruidPlugin(p.get("jdbcUrl1"), p.get("user"), p.get("password").trim());
	}
	private static Prop loadConfig() {
		try {
			// 优先加载生产环境配置文件
			return PropKit.use("jfinal_club_config_pro.txt");
		} catch (Exception e) {
			// 找不到生产环境配置文件，再去找开发环境配置文件
			return PropKit.use("jfinal_club_config_dev.txt");
		}
	}
}
